package com.example.jf.jfapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.jf.jfapp.R;
import com.example.jf.jfapp.bean.request.DongTaiListRequest;
import com.example.jf.jfapp.bean.response.CommentResponse;
import com.example.jf.jfapp.widget.GlideCircleTransform;

/**
 * Created by jf on 2017/7/29.
 */

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ItemViewHolder> {
private CommentResponse mCommentResponse;
private ActionClickListener mClickListener;

private Context mContext;

public void setmClickListener(ActionClickListener mClickListener) {
        this.mClickListener = mClickListener;
        }

public CommentAdapter(Context context, CommentResponse commentResponse) {
        this.mCommentResponse = commentResponse;
        this.mContext = context;
        }

@Override
public CommentAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
        }

@Override
public void onBindViewHolder(final CommentAdapter.ItemViewHolder holder, final int position) {
        CommentResponse.ResultBean bean = mCommentResponse.getResult().get(position);
        holder.tv_name.setText(bean.getUser().getUsername());

        Glide.with(holder.itemView.getContext())
        .load(bean.getUser().getImageUrl())
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .transform(new GlideCircleTransform(holder.itemView.getContext()))
        .into(holder.iv_head);


        holder.tv_date.setText(bean.getCreatedAt());

        LinearLayout replyView = createReplyView(bean);
        holder.content.addView(replyView);

        }

/**
 * 创建回复&评论的布局
 * @param resultBean
 * @return
 */
private LinearLayout createReplyView(CommentResponse.ResultBean resultBean) {
        LinearLayout view = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.item_comment_reply, null);
        TextView tv = view.findViewById(R.id.tv);
        tv.setText(resultBean.getContent() + "");//设置内容
        if (resultBean.getResultBean() != null) {
        View childView = createReplyView(resultBean.getResultBean());
        view.addView(childView);
        }
        return view;
        }


@Override
public int getItemCount() {
        return mCommentResponse.getResult().size();
        }

class ItemViewHolder extends RecyclerView.ViewHolder {
    ImageView iv_head;
    TextView tv_name;
    LinearLayout content;
    TextView tv_date;

    public ItemViewHolder(View itemView) {
        super(itemView);
        iv_head = itemView.findViewById(R.id.iv_head);
        tv_name = itemView.findViewById(R.id.tv_name);
        content = itemView.findViewById(R.id.content);
        tv_date = itemView.findViewById(R.id.tv_date);
    }
}

public interface ActionClickListener {
    void commentClick(DongTaiListRequest resultBean, int position);

    void pariseClick(DongTaiListRequest resultBean, int position);
}

}
